//
//  WBTarBarController.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/12.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit

class WBTarBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildrenViewControllers()
        
        let tabbar = WBTabBar()
        self.setValue(tabbar, forKey: "tabBar")
        
        //监听点击事件
        //注意循环引用,加上[weak self]
        tabbar.composeClosure = {[weak self] in
            
            print("撰写按钮被点击了")
            let composeView  = WBComposeView()
            
//            UIApplication.shared.keyWindow?.addSubview(composeView)
            composeView.composeViewShow(fromVC: self)
        }
        
    }


    
    private func addChildrenViewControllers(){
        addChildViewController(vc: WBHomeController(), title: "首页", imgName: "tabbar_home",index:0)
        addChildViewController(vc: WBMessageController(), title: "消息", imgName: "tabbar_message_center",index:1)
        addChildViewController(vc: WBDiscoverController(), title: "发现", imgName: "tabbar_discover",index:2)
        addChildViewController(vc: WBProfileController(), title: "我", imgName: "tabbar_profile",index:3)

    }
    
    
    private func addChildViewController(vc:UIViewController,title:String,imgName:String,index:Int){
        vc.tabBarItem.image = UIImage(named: imgName)
        vc.tabBarItem.selectedImage = UIImage(named: imgName+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeValue = nil
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
        vc.tabBarItem.tag = index
        let nav = WBBaseNavController(rootViewController: vc)
        
        self.addChildViewController(nav)
        
    }
    //item抖动的动画
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        var index = 0
        //获取被点击的item
        for subview in tabBar.subviews{
            if subview.isKind(of: NSClassFromString("UITabBarButton")!){
                if index == item.tag {
                    for v in subview.subviews{
                        if v.isKind(of: NSClassFromString("UITabBarSwappableImageView")!){
                            //做动画,先缩小在恢复原来大小
                            v.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                print("完事儿")
                            })
                        }
                    }
                }
                index += 1
            }
        }
        
    }
}
